package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")    //也可以使用配置文件的方式定义下面的属性，懒，不做这个了
@Component
@Data
public class AlipayTemplate {
    private String app_id = "2021004894654646";    //在支付宝创建的应用的id

    //商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcw 略略~~";
    //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 略略~~";

    //服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    //private String notify_url = "http://f3mwhc.natappfree.cc/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
    private String notify_url = "http://bvwxea.natappfree.cc/payed/notify";

    //页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    //private String return_url = "http://f3mwhc.natappfree.cc/alipay_trade_page_pay_JAVA_UTF_8/return_url.jsp";
    private String return_url = "http://member.gulimall.com/memberOrder.html";    //跳转到当前会员的订单列表页面

    private String sign_type = "RSA2";    //签名方式
    private String charset = "utf-8";    //字符编码格式
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";    //支付宝网关
    private String timeOut = "1m";    //付款的超时时间

    /**
     * 付款
     */
    public String pay(PayVo vo) throws AlipayApiException {
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        String out_trade_no = vo.getOut_trade_no();    //商户订单号，商户网站订单系统中唯一订单号，必填
        String total_amount = vo.getTotal_amount();    //付款金额，必填
        String subject = vo.getSubject();    //订单名称，必填
        String body = vo.getBody();    //商品描述，可空

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeOut +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);
        return result;
    }
}
